package br.com.serialmanager.model.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.serialmanager.util.DatabaseHelper;
import br.com.serialmanager.util.DatabaseHelper.Software;

public class SoftwareDao {

	private DatabaseHelper helper;
	private SQLiteDatabase db;
	
	public SoftwareDao(Context context) {
		this.helper = new DatabaseHelper(context);
	}
	
	private SQLiteDatabase getDb() {
		if (this.db == null) {
			this.db = this.helper.getWritableDatabase();
		}
		return this.db;
	}
	
	public List<Map<String, Object>> listarSoftwaresPorMascara(String idMascara) {
		List<Map<String, Object>> lista = new ArrayList<Map<String, Object>>();
		Cursor cursor = this.getDb().query(Software.TABELA, Software.COLUNAS, "soft_masc_id = ?", new String[]{idMascara}, null, null, Software._ID);
		if (cursor.moveToFirst()) {
			do {
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("soft_id", cursor.getInt(0));
				item.put("nome_software", cursor.getString(1));
				item.put("numero_de_serie", cursor.getString(3));
				item.put("data_validade", new Date(cursor.getLong(2)));
				item.put("masc_formato", cursor.getInt(4));
				lista.add(item);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return lista;
	}

	public List<Map<String, Object>> listarSoftwares() {
		List<Map<String, Object>> lista = new ArrayList<Map<String, Object>>();
		Cursor cursor = this.getDb().query(Software.TABELA, Software.COLUNAS, null, null, null, null, Software._ID);
		if (cursor.moveToFirst()) {
			do {
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("soft_id", cursor.getInt(0));
				item.put("nome_software", cursor.getString(1));
				item.put("numero_de_serie", cursor.getString(3));
				if (cursor.getLong(2) != 0) {
					item.put("data_validade", new Date(cursor.getLong(2)));
				} else {
					item.put("data_validade", null);
				}
				item.put("masc_formato", cursor.getInt(4));
				lista.add(item);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return lista;
	}
	
	public boolean salvarSoftware(br.com.serialmanager.model.bean.Software software) {
		long resultado;
		ContentValues values = new ContentValues();
		values.put(Software.NOME, software.getNome());
		if (software.getValidade() != null) {
			values.put(Software.DATA_VALIDADE, software.getValidade().getTime());
		} else {
			values.put(Software.DATA_VALIDADE, 0);
		}
		values.put(Software.NUMERO_DE_SERIE, software.getNumeroDeSerie());
		values.put(Software.MASCARA_ID, software.getMascaraId());
		if (software.getId() != null) {
			values.put(Software._ID, software.getId());
			resultado = this.getDb().update(Software.TABELA, values, "soft_id = ?", new String[]{software.getId().toString()});
		} else {
			resultado = this.getDb().insert(Software.TABELA, null, values);
		}
		return resultado != -1 ? true : false;
	}

	public br.com.serialmanager.model.bean.Software carregarPorId(String idSoftware) {
		// TODO Auto-generated method stub
		br.com.serialmanager.model.bean.Software soft = null;
		Cursor cursor = this.getDb().query(Software.TABELA, Software.COLUNAS, "soft_id = ?", new String[]{idSoftware}, null, null, null);
		if (cursor.moveToFirst()) {
			soft = new br.com.serialmanager.model.bean.Software(cursor.getInt(0), cursor.getString(1), new Date(cursor.getLong(2)), cursor.getString(3), cursor.getInt(4));
		}
		return soft;
	}
	
	public br.com.serialmanager.model.bean.Software carregarPorNumeroDeSerie(String numeroDeSerie) {
		br.com.serialmanager.model.bean.Software soft = null;
		Cursor cursor = this.getDb().query(Software.TABELA, Software.COLUNAS, "soft_nserie = ?", new String[]{numeroDeSerie}, null, null, null);
		if (cursor.moveToFirst()) {
			soft = new br.com.serialmanager.model.bean.Software(cursor.getInt(0), cursor.getString(1), new Date(cursor.getLong(2)), cursor.getString(3), cursor.getInt(4));
		}
		return soft;
	}
	
	public boolean apagarSoftware(String id) {
		long resultado = this.getDb().delete(Software.TABELA, "soft_id = ?", new String[]{id});
		return resultado > 0 ? true : false;
	}
}
